/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.token.dao.LoginDAO;
import com.opgea.token.domain.constants.ApplicationConstants;
import com.opgea.token.domain.entities.Login;
import com.opgea.token.service.LoginService;
import com.opgea.token.web.dto.LoginDTO;

/**
 * 
 * @author Ramesh
 */
@Service
@Transactional
public class LoginServiceImpl implements LoginService {

    private Logger logger = Logger.getLogger(LoginServiceImpl.class);

    @Autowired
    private LoginDAO loginDAO;

    @Override
    public Boolean isAuthenticUser(String loginId, String password) {
	logger.info("LoginDAO >> isAuthentic: " + loginId + " | " + password);
	Boolean authentic = false;
	Login login = loginDAO.findByLoginId(loginId);
	if (login != null) {
	    if (login.getPassword().equals(password)) {
		authentic = true;
	    }
	}
	return authentic;
    }

    public LoginDTO create(LoginDTO loginDTO) {
	Login login = new Login();
	loginDTO.unmarshall(login);
	login.setIsActive(Boolean.TRUE);
	if (loginDTO.getId() == 0) {
	    loginDAO.create(login);
	} else {
	    loginDAO.update(login);
	}
	loginDTO.setId(login.getId());
	return loginDTO;
    }

    public LoginDTO update(LoginDTO loginDTO) {
	Login login = new Login();
	loginDTO.unmarshall(login);
	loginDAO.update(login);
	loginDTO.setId(login.getId());
	return loginDTO;
    }

    public LoginDTO find(Long id) {
	Login login = loginDAO.find(id);
	LoginDTO loginDTO = new LoginDTO();
	loginDTO.marshall(login);
	return loginDTO;
    }

    public LoginDTO findByLoginId(String loginId) {
	Login login = loginDAO.findByLoginId(loginId);
	LoginDTO loginDTO = new LoginDTO();
	loginDTO.marshall(login);
	return loginDTO;
    }

    public List<LoginDTO> findAll() {
	List<Login> loginList = loginDAO.findAll();
	List<LoginDTO> loginDTOList = new ArrayList<LoginDTO>();
	for (Login login : loginList) {
	    LoginDTO dto = new LoginDTO();
	    dto.marshall(login);
	    loginDTOList.add(dto);
	    logger.info(dto);
	}
	return loginDTOList;
    }

    public Map<String, Object> verify(String loginId, String password) {
	Map<String, Object> responseMap = new HashMap<String, Object>();
	responseMap.put(ApplicationConstants.LOGIN_ID, Boolean.TRUE);
	responseMap.put(ApplicationConstants.PASSWORD, Boolean.TRUE);
	responseMap.put(ApplicationConstants.EXCEPTION, Boolean.FALSE);

	Login login = loginDAO.findByLoginId(loginId);
	if (login == null) {
	    responseMap.put(ApplicationConstants.LOGIN_ID, Boolean.FALSE);
	}
	if (login != null && !login.getPassword().equals(password)) {
	    responseMap.put(ApplicationConstants.PASSWORD, Boolean.FALSE);
	}
	if (login != null) {
	    LoginDTO loginDTO = new LoginDTO();
	    loginDTO.marshall(login);
	    responseMap.put(ApplicationConstants.OBJECT, loginDTO);
	}
	return responseMap;
    }

}
